package com.problem.leetcode;

import com.google.common.base.Stopwatch;

import java.util.Arrays;

/**
 * 解法:1.使用排序 奇数位相加
 * 2.由于元素个数有限 所以使用计数排序法，能进一步提升效率
 * @author didi
 * @DESCRIPTION
 * @create 2019-05-13 10:58
 **/

public class Problem561 {
    public int arrayPairSum(int[] nums) {
        Arrays.sort(nums);
        int sum = 0;
        for (int index = 0; index < nums.length; index += 2) {
            sum += nums[index];
        }
        return sum;
    }

    public static void main(String[] args) {
        Problem561 problem561 = new Problem561();
        Stopwatch stopwatch = Stopwatch.createStarted();
        for (int index = 0; index < 10000; index++) {
            problem561.arrayPairSum(new int[]{1,4,3,2});
        }
        stopwatch.stop();
        System.out.println(stopwatch.toString());
    }

}
